<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 充值规则
 * Class RechargeRuleController
 * @package App\Http\Controllers\Admin
 */
class RechargeRuleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $list = DB::table('recharge_rule')->orderBy('need_money', 'asc')->get();
        return view('admin.recharge.rule',['list' => $list]);
    }

    public function edit($id)
    {
        $detail = DB::table('recharge_rule')->find($id);
        return view('admin.recharge.edit', ['detail' => $detail]);
    }

    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'need_money' => ['required','int'],
            'gift_ly' => ['required','int'],
            'gift_wf' => ['required','int'],
            'gift_hb' => ['required','int'],
        ]);
        if($id != 0){
            // 更新
            DB::table('recharge_rule')
                ->where('id', $id)
                ->update([
                    'need_money' => $request->need_money,
                    'gift_ly' => $request->gift_ly,
                    'gift_wf' => $request->gift_wf,
                    'gift_hb' => $request->gift_hb,
                ]);
        }else{
            // 创建
            DB::table('recharge_rule')
                ->insert([
                    'need_money' => $request->need_money,
                    'gift_ly' => $request->gift_ly,
                    'gift_wf' => $request->gift_wf,
                    'gift_hb' => $request->gift_hb,
                ]);
        }
        return successResp('');
    }

    public function destroy($id)
    {
        DB::table('recharge_rule')
            ->whereIn('id', explode(',', $id))
            ->delete();
        return successResp('');
    }
}
